<?php
namespace App\Modules\Relationship\Models;

use Illuminate\Support\Facades\DB;
use App\Http\Model\Base;

class SweetTitleModel extends Base {

    protected $table = "rs_sweet_title";
    protected $fields = ['id', 'group', 'title'];
    protected $tablePrefix = '';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    public function getTitles () : array {
        $condition = array(
            "is_deleted"=>0
        );
        $return = $this->getListByCondition($condition, $this->fields);
        if (!$return) {
            throw new \Exception("密友称号数据不存在");
        }
        $return = array_column($return, null, 'id');
        return $return;
    }

    public function createTitle ($userTitle,$friendTitle) {
        $group = time();
        $insert[] = array('group'=>$group, 'title'=>$userTitle);
        $insert[] = array('group'=>$group, 'title'=>$friendTitle);
        return DB::table($this->table)->insert($insert);
    }

    public function updateTitle ($id, $title = '') {
        return DB::table($this->table)->where('id', $id)->update(array('title'=>$title));
    }

    public function deleteTitle ($group) {

        return DB::table($this->table)->where('group', $group)->update(array('is_deleted'=>1));

    }
}

